package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.CateHouse;
import com.wuming.house.model.GroupHouse;
import com.wuming.house.model.House;
import com.wuming.house.model.Secondhouse;
import com.wuming.house.service.CateHouseService;
import com.wuming.house.service.GroupHouseService;
import com.wuming.house.service.SecondhouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/9/4 16:57
 * @Description:
 */
@Controller
@RequestMapping("/web/cateHouse")
public class CateHouseController extends BaseController {

    @Autowired
    private CateHouseService cateHouseService;

    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(CateHouse cateHouse) {
        try {
            CateHouse ch = cateHouseService.getHouseCateByCateIdAndHouseId(cateHouse.getCateId(), cateHouse.getHouseId());
            if (null == ch) {
                cateHouseService.saveCateHouse(cateHouse);
                return new JsonResult(null, "保存新房分类成功", true);
            } else {
                return new JsonResult(null, "保存新房分类失败,该新房存在在该分类", false);
            }
        } catch (Exception e) {
            logger.error("保存新房分类失败,失败信息:{}", e.getMessage());
        }
        return new JsonResult(null, "发生业务异常", false);
    }

    @RequestMapping("/toCateHouses/{cateId}")
    public String toGroupHouses(@PathVariable Long cateId, Model model) {
        model.addAttribute("cateId", cateId);
        if (cateId == 1){
            return "/house/cateHouse";
        }else{
            return "/secondhouse/cateSecondhouse";
        }
    }

    @ResponseBody
    @RequestMapping("/list")
    public CommonResult list(@RequestParam Integer cateId,@RequestParam(required = false)String houseName,
                             @RequestParam(required = false)Integer price,@RequestParam(defaultValue = "1") int pageIndex,
                             @RequestParam(required = false)String area,@RequestParam(defaultValue = "10") int pageSize) {
        try {
            List<House> houses = new ArrayList<>();
            List<CateHouse> cateHouses = cateHouseService.getHouseByParams(cateId, "", "", "", "", pageIndex, pageSize);
            int total = cateHouseService.getTotal(cateId);
            for (CateHouse cateHouse : cateHouses) {
                houses.add(cateHouse.getHouse());
            }
            return CommonResult.createSuccessResult(houses, total, "查询分类下的楼盘信息成功");
        } catch (Exception e) {
            logger.error("查询分类下的楼盘信息失败,失败信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询分类下的楼盘信息失败");
        }
    }

    @ResponseBody
    @RequestMapping("/delete/{cateId}/{houseId}")
    public JsonResult delete(@PathVariable Integer cateId,@PathVariable Long houseId){
        try {
            cateHouseService.deleteByParams(houseId,cateId);
            return new JsonResult(null,"删除分类楼盘成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(null,"删除分类楼盘失败",false);
        }
    }
}
